package com.sunlands.deskmate.message.handler.receiver;

import com.sunlands.deskmate.message.enums.MessageType;
import org.springframework.stereotype.Component;
import java.util.Map;

/**
 * @author anjunling@sunlands.com
 * 消息接收器工厂类，用于管理所有的消息接收器
 */
@Component
public class MessageReceiverFactory {

    private volatile Map<String, Receiver> receiverMap;

    public MessageReceiverFactory(Map<String, Receiver> receiverMap) {
        this.receiverMap = receiverMap;
    }

    public Receiver getReceiver(String msgType) {
        //根据消息类似获取对应的消息接收器
        Class<Receiver> receiver = MessageType.of(msgType).getReceiver();
        return receiverMap.values().stream()
                .filter(r -> r.getClass().getName().toLowerCase().equals(receiver.getName().toLowerCase()))
                .findFirst()
                .orElseThrow(NullPointerException::new);
    }

}
